package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.LatentClient;
import cn.wolfcode.crm.domain.Latentclientplan;
import cn.wolfcode.crm.mapper.LatentClientMapper;
import cn.wolfcode.crm.query.LatentClientQueryObject;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.service.ILatentClientPlaService;
import cn.wolfcode.crm.service.ILatentClientService;
import cn.wolfcode.crm.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class LatentClientService implements ILatentClientService{

    @Autowired
    private LatentClientMapper latentClientMapper;

    @Autowired
    private ILatentClientPlaService planService;

    public void insert(LatentClient record) {
        Employee currentUser = UserContext.getCurrentUser();
        //把当前用户设置成创建人
        record.setInputUser(currentUser);
        //把当前用户设为负责人
        record.setInChargeUser(currentUser);
        latentClientMapper.insert(record);
    }

    public LatentClient selectByPrimaryKey(Long id) {
        return latentClientMapper.selectByPrimaryKey(id);
    }


    public void changStatus(Long id) {
        //查询到当前操作的客户对象
        LatentClient latentClient = latentClientMapper.selectByPrimaryKey(id);
        //把其状态设为资源池待开发状态
        latentClient.setStatus(LatentClient.CLIENT_RESOURCE);
        //更新状态
        latentClientMapper.changeClient(latentClient);
    }

    public void turnState(Long id) {
        //查询到当前操作的客户对象
        LatentClient latentClient = latentClientMapper.selectByPrimaryKey(id);
        //把其状态设为资源池待开发状态
        latentClient.setStatus(LatentClient.CLIENT_STATE);
        //更新状态
        latentClientMapper.change2State(latentClient);
    }

    public PageResult selectClientByStatus(LatentClientQueryObject qo) {
        int total = latentClientMapper.resourceQueryForCount(qo);
        if (total > 0) {
            List<LatentClient> rows = latentClientMapper.selectClientByStatus(qo);
            return new PageResult(rows,total);
        }
        return new PageResult();
    }

    public PageResult selectStateByStatus(LatentClientQueryObject qo) {
        int total = latentClientMapper.stateQueryForCount(qo);
        if (total > 0) {
            List<LatentClient> rows = latentClientMapper.selectStateClientByStatus(qo);
            return new PageResult(rows,total);
        }
        return new PageResult();
    }

    @Override
    public LatentClient selectApplicantById(Long id) {
        return latentClientMapper.selectByPrimaryKey(id);
    }

    public void plan(Long id) {
        //查询到当前操作的客户对象
        LatentClient latentClient = latentClientMapper.selectByPrimaryKey(id);
        //把其状态设为尝试开发状态
        latentClient.setStatus(LatentClient.CLIENT_PLAN);
        //更新状态
        latentClientMapper.changePlan(latentClient);
        //把潜在客户表的客户名和负责人添加到客户开发对象中
        Latentclientplan plan = new Latentclientplan();
        //把潜在客户表中的客户名,设置到客户开发计划表中
        plan.setInputtime(new Date());
        plan.setLatentclient(latentClient);
        //把当前用户,设置为客户开发计划表中的录入人
        Employee currentUser = UserContext.getCurrentUser();
        plan.setInputUser(currentUser);
        //保存plan对象
        planService.insert(plan);
    }

    @Override
    public void deleteLatentClient(Long id) {
        latentClientMapper.deleteByPrimaryKey( id );
    }

    @Override
    public void changeStateTransfer(Long id) {
        latentClientMapper.changeStateTransfer(id);
    }

    @Override
    public void changeState2Latent(Long id) {
        latentClientMapper.changeState2Latent(id);
    }


    @Override
    public List<LatentClient> listAll() {
        return latentClientMapper.listAll();
    }

    public void updateByPrimaryKey(LatentClient record) {
        latentClientMapper.updateByPrimaryKey(record);
    }

    public PageResult query(LatentClientQueryObject qo) {
        //1:查总条数
        int total = latentClientMapper.queryForCount(qo);
        if (total > 0) {
            List<LatentClient> rows = latentClientMapper.queryForList(qo);
            return new PageResult(rows,total);
        }
        return new PageResult();
    }


    public void changeState(Long id) {
        latentClientMapper.changeState(id);
    }

        //状态查用户
    public List<LatentClient> selectByStatus(int status) {
        return latentClientMapper.selectByStatus(status);
    }

    public void deleteByPrimaryKey(Long id) {
        latentClientMapper.deleteByPrimaryKey(id);
    }

}
